package doudizhu;

import java.util.*;

/**
5. 编程题

 使用集合实现斗地主游戏的部分功能，要求如下：

 （1）首先准备 54 张扑克牌并打乱顺序。

 （2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。

 （3）查看三个玩家手中的扑克牌和底牌。

 （4）其中玩家手中的扑克牌需要按照大小顺序打印，规则如下：

    手中扑克牌从大到小的摆放顺序：大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
 */
public class DouDiZhuTest {
    public static void main(String[] args) {
        
        //创建一个管理员
        DouDiZhuTest ddzt = new DouDiZhuTest();

        System.out.println("----------开始准备一副Poker牌------------");

        Pokers pokers = new Pokers();
        //总牌堆
        List<Poker> list = pokers.getList();
        for (int i=0;i<list.size();i++) {
            System.out.print(list.get(i).getBrand());
        }
        //洗牌
        System.out.println();
        Collections.shuffle(list);
        for (int i=0;i<list.size();i++) {
            System.out.print(list.get(i).getBrand());
        }
        System.out.println();

        System.out.println("-----------牌已经切好------------------");

        System.out.println("----------请三位选手登场---------------");

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入第一位选手的姓名");

        String playerName1 = sc.nextLine();

        System.out.println("请输入第一位选手的号码");

        int playerId1 = sc.nextInt();

        Player player1 = new Player(playerId1,playerName1);

        System.out.println("请输入第二位选手的姓名");

        Scanner sc1 = new Scanner(System.in);

        String playerName2 = sc1.nextLine();

        System.out.println("请输入第二位选手的号码");

        int playerId2 = sc1.nextInt();

        Player player2 = new Player(playerId1,playerName1);

        System.out.println("请输入第三位选手的姓名");

        Scanner sc2 = new Scanner(System.in);

        String playerName3 = sc2.nextLine();

        System.out.println("请输入第三位选手的号码");

        int playerId3 = sc2.nextInt();

        Player player3 = new Player(playerId3,playerName3);

//        System.out.println("现在随机进行先后手");
//
//        int a = (int)(Math.random()*100);
//
//        int b = (int)(Math.random()*100);
//
//        int c = (int)(Math.random()*100);

        List<Poker> player1List = new ArrayList<>();

        List<Poker> player2List = new ArrayList<>();

        List<Poker> player3List = new ArrayList<>();

        List<Poker> otherList = new ArrayList<>();
        //因为要留三张牌做底牌
        for (int i=0;i<list.size();i++) {
            if (i>=51){
                otherList.add(list.get(i));
            }else {
                if (i%3==0) {
                    player1List.add(list.get(i));
                }else if (i%3==1) {
                    player2List.add(list.get(i));
                }else if (i%3==2){
                    player3List.add(list.get(i));
                }
            }
        }
        //对手牌进行排序
        ddzt.sort(player1List);
        ddzt.sort(player2List);
        ddzt.sort(player3List);
        ddzt.sort(otherList);
        player1.setPokerList(player1List);
        player2.setPokerList(player2List);
        player3.setPokerList(player3List);
        //看牌
        System.out.println("请问ddzt你现在想要看谁的牌");
        System.out.println("player1的Id为：" + playerId1);
        System.out.println("player2的Id为：" + playerId2);
        System.out.println("player3的Id为：" + playerId3);
        System.out.println("请输入Id，如果输其他数字表示查看底牌，输入-1表示退出");
        Scanner sc4 = new Scanner(System.in);
        while (true) {
            int whichBody = sc4.nextInt();
            if (playerId1==whichBody) {
                ddzt.seePokers(player1List);
            }else if (playerId2==whichBody) {
                ddzt.seePokers(player2List);
            }else if (playerId3==whichBody) {
                ddzt.seePokers(player3List);
            }else if (-1==whichBody){
                break;
            }else {
                ddzt.seePokers(otherList);
            }
        }
    }

    private void seePokers(List<Poker> list) {
        System.out.println(list.toString());
    }

    //在实体类里实现Comparable接口进行排序
    private void sort(List<Poker> list) {
        Collections.sort(list);
    }

}
